package fei.typhoon.engine.load;

import java.io.OutputStream;
import java.nio.Buffer;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Bitmap.Config;
import android.os.Parcel;
import android.util.DisplayMetrics;
import fei.typhoon.engine.math.FeiPolygon;

public class FeiBitmap {
	private Bitmap bitmap = null;
	private FeiPolygon shape = null;

	public FeiBitmap(Bitmap bitmap) {
		this(bitmap, (bitmap == null) ? (FeiPolygon) null : new FeiPolygon(0,
				0, bitmap.getWidth(), 0, bitmap.getWidth(), bitmap.getHeight(),
				0, bitmap.getHeight()));
	}

	public FeiBitmap(Bitmap bitmap, float... vertexs) {
		this(bitmap, (bitmap == null) ? (FeiPolygon) null : new FeiPolygon(
				vertexs));
	}

	public FeiBitmap(Bitmap bitmap, FeiPolygon shape) {
		this.bitmap = bitmap;
		this.shape = shape;
	}

	public Bitmap getBitmap() {
		return bitmap;
	}

	public FeiPolygon getShape() {
		return shape;
	}

	public void setShape(FeiPolygon shape) {
		this.shape = shape;
	}

	public void setShape(float... vertexs) {
		this.shape = new FeiPolygon(vertexs);
	}

	// 以下是源自bitmap的方法
	public int getWidth() {
		return ((bitmap == null) ? 0 : bitmap.getWidth());
	}

	public int getHeight() {
		return ((bitmap == null) ? 0 : bitmap.getHeight());
	}

	public void recycle() {
		if (bitmap != null) {
			bitmap.recycle();
		}
	}

	public boolean compress(CompressFormat format, int quality,
			OutputStream stream) {
		if (bitmap != null) {
			return bitmap.compress(format, quality, stream);
		}
		return false;
	}

	public Bitmap copy(Config config, boolean isMutable) {
		if (bitmap != null) {
			return (bitmap.copy(config, isMutable));
		}
		return null;
	}

	public void copyPixelsFromBuffer(Buffer src) {
		if (bitmap != null) {
			bitmap.copyPixelsFromBuffer(src);
		}
	}

	public void copyPixelsToBuffer(Buffer dst) {
		if (bitmap != null) {
			bitmap.copyPixelsToBuffer(dst);
		}
	}

	public int describeContents() {
		if (bitmap != null) {
			return bitmap.describeContents();
		}
		return -1;
	}

	public void eraseColor(int c) {
		if (bitmap != null) {
			bitmap.eraseColor(c);
		}
	}

	public Bitmap extractAlpha() {
		if (bitmap != null) {
			return bitmap.extractAlpha();
		}
		return null;
	}

	public Bitmap extractAlpha(Paint paint, int[] offsetXY) {
		if (bitmap != null) {
			return bitmap.extractAlpha(paint, offsetXY);
		}
		return null;
	}

	public Config getConfig() {
		if (bitmap != null) {
			return bitmap.getConfig();
		}
		return null;
	}

	public int getDensity() {
		if (bitmap != null) {
			return bitmap.getDensity();
		}
		return -1;
	}

	public int getRowBytes() {
		if (bitmap != null) {
			return bitmap.getRowBytes();
		}
		return 0;
	}

	public int getPixel(int x, int y) {
		if (bitmap != null) {
			return bitmap.getPixel(x, y);
		}
		return -1;
	}

	public void getPixels(int[] pixels, int offset, int stride, int x, int y,
			int width, int height) {
		if (bitmap != null) {
			bitmap.getPixels(pixels, offset, stride, x, y, width, height);
		}
	}

	public byte[] getNinePatchChunk() {
		if (bitmap != null) {
			return bitmap.getNinePatchChunk();
		}
		return null;
	}

	public int getScaledHeight(Canvas canvas) {
		if (bitmap != null) {
			return bitmap.getScaledHeight(canvas);
		}
		return 0;
	}

	public int getScaledHeight(DisplayMetrics metrics) {
		if (bitmap != null) {
			return bitmap.getScaledHeight(metrics);
		}
		return 0;
	}

	public int getScaledHeight(int targetDensity) {
		if (bitmap != null) {
			return bitmap.getScaledHeight(targetDensity);
		}
		return 0;
	}

	public int getScaledWidth(Canvas canvas) {
		if (bitmap != null) {
			return bitmap.getScaledWidth(canvas);
		}
		return 0;
	}

	public int getScaledWidth(DisplayMetrics metrics) {
		if (bitmap != null) {
			return bitmap.getScaledWidth(metrics);
		}
		return 0;
	}

	public int getScaledWidth(int targetDensity) {
		if (bitmap != null) {
			return bitmap.getScaledWidth(targetDensity);
		}
		return 0;
	}

	public void setDensity(int density) {
		if (bitmap != null) {
			bitmap.setDensity(density);
		}
	}

	public void setPixel(int x, int y, int color) {
		if (bitmap != null) {
			bitmap.setPixel(x, y, color);
		}
	}

	public void setPixels(int[] pixels, int offset, int stride, int x, int y,
			int width, int height) {
		if (bitmap != null) {
			bitmap.setPixels(pixels, offset, stride, x, y, width, height);
		}
	}

	public boolean hasAlpha() {
		if (bitmap != null) {
			return bitmap.hasAlpha();
		}
		return false;
	}

	public boolean isMutable() {
		if (bitmap != null) {
			return bitmap.isMutable();
		}
		return false;
	}

	public boolean isRecycled() {
		if (bitmap != null) {
			return bitmap.isRecycled();
		}
		return true;
	}

	public void prepareToDraw() {
		if (bitmap != null) {
			bitmap.prepareToDraw();
		}
	}

	public void writeToParcel(Parcel p, int flags) {
		if (bitmap != null) {
			bitmap.writeToParcel(p, flags);
		}
	}

	// 以下是源自FeiPolygon的方法
	public void setVertexs(float... coor) {
		if (shape != null) {
			shape.set(coor);
		}
	}

	public float[] getVertexs() {
		if (shape != null) {
			return shape.getVertexs();
		}
		return null;
	}

	public boolean pointInRegion(float x, float y) {
		if (shape != null) {
			return shape.pointInRegion(x, y);
		}
		return false;
	}

	public void resetVertexs() {
		if (shape != null) {
			shape.reset();
		}
	}

	public void matrix(Matrix matrix) {
		if (shape != null) {
			shape.matrix(matrix);
		}
	}
}
